package com.jmb.api.common.utils;

import com.alibaba.fastjson2.JSONArray;
import com.jmb.api.common.exception.JsonException;
import com.sun.istack.NotNull;

public class JsonUtil {
    public static void isJsonPath(String jsonPath){
        notNull(jsonPath, "参数 jsonPath 为空");
        notEmpty(jsonPath, "参数 jsonPath 为空");
        // 判断是否以 $ 开头
        if (!jsonPath.startsWith("$")){
            throw new JsonException("jsonPath 必须以 $ 开头");
        }

    }
    public static void isXpath(String xpath){
        notNull(xpath, "参数 xpath 为空");
        notEmpty(xpath, "参数 xpath 为空");
        // 验证 xpath 是否以 / //开头
        if (!xpath.startsWith("/")|| xpath.startsWith("//")){
            throw new JsonException("xpath 必须以 / 或 // 开头");
        }
    }
    public static void notNull(Object object,String msg){
        if (object == null){
            throw new JsonException(msg);
        }
    }
    public static void notEmpty(String s, String msg)  {
        notNull(s, "参数 字符串 为空");
        if (s.isEmpty()) {
            throw new JsonException(msg);
        }
    }
    public static void notEmpty(JSONArray array, String msg)  {
        notNull(array, "参数 JSON数组为空");
        if (array.isEmpty()) {
            throw new JsonException(msg);
        }
    }
}
